#ifndef __POKER_H__
#define __POKER_H__

#include <button.h>
#include <sdl_net.h>

#define DEBUG0(...)
#define DEBUG1(...)     fprintf(stderr, __VA_ARGS__);
#define DEBUG2(...)     perror(__VA_ARGS__);

#define ERRP(con, ret, flag, ...)   do          \
            {                                   \
                if (con)                        \
                {                               \
                    DEBUG##flag(__VA_ARGS__)    \
                    ret;                        \
                }                               \
            } while (0)


#define GHOSTS          0x0
#define HEARTS          0x1
#define SPADES          0x2
#define CLUBS           0x3
#define DIAMONDS        0x4
#define ASSISTANT       0x5

#define POKER_NUM           54      
#define CARD_TOTAL          108
#define CARD_PLAYER         50
#define CARD_LORD_PLAYER    58
#define CARD_SELL           22
#define CARD_HAND           18
#define CARD_BOTTOM         8
#define CARD_DESKTOP        32
#define CARD_ROW            8

#define NONE                200

//gui
#define WIDTH               800
#define HEIGHT              600
#define BPP                 32

#define DESKTOP_W           560
#define DESKTOP_H           600

#define CARD_W              50
#define CARD_H              71

#define CARD_SPACE          15
#define CARD_BORDER_SPACE   5

#define MARGIN              10


enum {THREE = 0, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE, TWO, BIG_GHOST = 0, SMALL_GHOST};

struct card_t {
    unsigned int color:3;           //牌的花色
    unsigned int num:4;             //牌的数字
    unsigned int selection:1;       //选中状态
    unsigned int hide:1;            //隐藏状态
};

struct player_t {
    unsigned char card[CARD_LORD_PLAYER];    //保存此玩家手中牌
    unsigned char level;                //保存此玩家打到几
    unsigned char hand_num;             //保存此玩家手里还有多少张牌
    unsigned char desktop[CARD_SELL];   //保存此玩家此次出的牌
    unsigned char desktop_num;          //保存此玩家此次出的牌的张数
};

struct tractor_t {
    struct card_t poker[CARD_TOTAL];            //扑克牌
    struct player_t player[2];                  //玩家信息 0 本地玩家 1 网络玩家
    unsigned char bottom[CARD_BOTTOM];          //底牌
    unsigned char desktop[CARD_SELL];           //桌面上的牌
    unsigned char desktop_num;                  //桌面上的牌的个数
    unsigned char point;                       //得分
    int lord:4;                                 //打什么主
    int who:2;                                  //打谁的主
    unsigned int ck_bottom:1;                      //是否扣底
    unsigned int alternate:1;                    //轮换出牌
    struct sdl_t *sdl;                          //图形库
    SDL_Surface *pic_btn_sell;                   //出牌的按钮图片
    SDL_Surface *pic_btn_cancel;                 //取消选中的按钮图片
    SDL_Surface *pic_btn_lord;                   //叫主的按钮图片
    SDL_Surface *pic_btn_bottom;                 //扣底的按钮图片
    SDL_Surface *bg;                       //牌的背景图片
    SDL_Surface *card_bg;                       //牌的背景图片
    SDL_Surface *card[ASSISTANT][TWO + 1];      //牌对应的图片
    struct button_t *btn_sell;
    struct button_t *btn_cancel;
    struct button_t *btn_lord;
    struct button_t *btn_bottom;
};

extern const struct card_t poker_value[POKER_NUM];
extern const char *poker_name[ASSISTANT][TWO + 1];

extern void shuffle(struct card_t *poker);
extern void deal_card(struct tractor_t *tractor);
extern void tidy_card(struct tractor_t *tractor, int total, unsigned char *player);

extern void destroy_sdl_res(struct tractor_t *tractor);
extern int load_sdl_res(struct tractor_t *tractor);
extern void show_tractor(struct tractor_t *tractor);
extern void mouse_handle(int button, int x, int y, void *data);

extern void sell_handle(void *);
extern void bottom_handle(void *);
extern void cancel_handle(void *);
extern void lord_handle(void *);

int sell_rule(struct tractor_t *tractor, unsigned char *card, int num);

#endif /* __POKER_H__ */
